package com.devops.admin.graphql;

import com.devops.admin.model.TableResult;
import com.devops.admin.po.DOSHelmOperationPo;
import com.devops.admin.po.DOSReleasePo;
import com.devops.admin.po.DOSValuePo;
import com.devops.admin.service.DOSK8sDeploymentService;
import com.devops.admin.service.DOSK8sPodService;
import com.devops.admin.service.DOSReleaseService;
import com.devops.admin.vo.*;
import com.devops.common.annotation.DOSLog;
import com.devops.constant.DevOpsConstant;
import com.netflix.graphql.dgs.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName DOSReleaseDataFetcher
 * @Description TODO
 * @Author heyabo
 * @Date 2022/8/4 11:01
 * @Version 1.0
 **/
@DgsComponent
@Slf4j
public class DOSReleaseDataFetcher {

    @Autowired
    DOSReleaseService dosReleaseService;
    @Autowired
    private DOSK8sDeploymentService dosk8sDeploymentService;
    @Autowired
    private DOSK8sPodService dosk8sPodService;



    /**
     * 部署release
     **/
    @DgsMutation
    @DOSLog(description = "部署release")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public DOSHelmOperationPo deployRelease(@InputArgument DOSReleaseParamVo param) {
        return dosReleaseService.installReleaseByRepoChart(param);
    }

    /**
     * 升级release
     **/
    @DgsMutation
    @DOSLog(description = "升级release")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public DOSHelmOperationPo upgradeRelease(@InputArgument DOSReleaseParamVo param) {
        return dosReleaseService.upgradeReleaseByRepoChart(param);
    }

    /**
     * 回滚release
     */
    @DgsMutation
    @DOSLog(description = "回滚release")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public DOSHelmOperationPo rollbackRelease(@InputArgument DOSReleaseParamVo param) {
        return dosReleaseService.rollbackRelease(param);
    }

    /**
     * 卸载release
     */
    @DgsMutation
    @DOSLog(description = "卸载release")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public DOSHelmOperationPo uninstallRelease(@InputArgument DOSReleaseParamVo param) {
        return dosReleaseService.uninstallRelease(param);
    }

    /**
     * @Author heyabo
     * @Description 重启release
     * @Date 15:14 2022/6/27
     **/
    @DgsMutation
    @DOSLog(description = "重启release")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public boolean restartRelease(@InputArgument Integer releaseId) throws IOException {

        return dosk8sDeploymentService.restartRelease(releaseId);
    }

    /**
     * 获取单个release的历史列表-分页
     */
    @DgsQuery
    @DOSLog(description = "获取单个release的历史列表")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public DOSHelmOperationPo historyRelease(@InputArgument DOSReleaseParamVo param) {
        return dosReleaseService.historyRelease(param);
    }

    /**
     * @Author heyabo
     * @Description 查询权限范围内release列表-分页
     * @Date 14:31 2022/3/24
     * @Param [releaseVo]
     * @return com.devops.admin.model.TableResult<com.devops.admin.po.DOSProjectPo>
     **/
    @DgsQuery
    @DOSLog(description = "查询权限范围内release列表")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public TableResult<DOSReleasePo> getReleaseList(@InputArgument DOSReleaseVo releaseVo, @InputArgument DOSPageVo pageVo){

        return dosReleaseService.getReleases(releaseVo,pageVo);
    }

    /**
     * @Author heyabo
     * @Description 查询values
     * @Date 2022/4/11
     * @Param [releaseVo]
     * @return java.lang.String
     **/
    @DgsQuery
    @DOSLog(description = "查询values")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public DOSValuePo getReleaseValues(@InputArgument DOSReleaseValuesVo valuesVo){

        return dosReleaseService.getValues(valuesVo);
    }

    /**
     * @Author heyabo
     * @Description 根据项目id和环境类型id查询release列表
     * @Date 13:51 2022/6/27
     * @Param [release]
     * @return java.util.List<com.devops.admin.po.DOSReleasePo>
     **/
    @DgsQuery
    @DOSLog(description = "根据项目id和环境类型id查询release列表")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.LOGIN_USER},logical = Logical.OR)
    public List<DOSReleasePo> getReleasesByProjectEnvType(@InputArgument DOSReleaseVo releaseVo){
        return dosReleaseService.getReleases(releaseVo);
    }

    /**
     * @Author heyabo
     * @Description 查询release的image和tag
     * @Date 11:37 2022/11/21
     * @Param [valuesVo]
     * @return java.lang.String
     **/
    @DgsQuery
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public String getImageTag(@InputArgument DOSReleaseValuesVo valuesVo){

        return dosReleaseService.getImageTag(valuesVo);
    }

    /**
     * @Author heyabo
     * @Description
     * @Date 14:20 2022/12/2
     * @Param [podName, podStatus, num]
     * @return org.reactivestreams.Publisher<java.lang.String>
     **/
    @DgsSubscription
    public Publisher<Integer> monitorPodStatus(@InputArgument  MonitorPodStatusVo podStatusVo){
        log.debug("**********************开始订阅了");
        return dosk8sPodService.monitorPodStatus(podStatusVo);
    }
}
